﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
using System.Xml;
using NASA.BeAMartian.Controls;
using System.IO;
using NASA.BeAMartian.Controls.News;
using System.Windows.Navigation;
using Microsoft.TownHall.ServiceClient;

namespace NASA.BeAMartian.Views.Learn
{
    public partial class Index : PhoneApplicationPage
    {
        object _selectedItem;
        public THServiceClient client = new THServiceClient();
        private CategoryList Categories = new CategoryList();

        public Index()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Index_Loaded);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            base.OnNavigatedTo(e);
        }

        void Index_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient xmlClient = new WebClient();
            xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
            xmlClient.DownloadStringAsync(new Uri("http://mars.jpl.nasa.gov/rss/newsdata.cfm", UriKind.Absolute));
        }

        void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XmlSerializer x = new XmlSerializer(typeof(NewsItems));

                using (XmlReader reader = XmlReader.Create(new StringReader(e.Result)))
                {
                    NewsItems ni = (NewsItems)x.Deserialize(reader);
                    lbWhatsNew1.ItemsSource =
                        (from n in ni
                            select n).Take(3);
                    pbProgress.IsIndeterminate = false;
                    pbProgress.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        private void Image_Loaded(object sender, RoutedEventArgs e)
        {
            Image img = sender as Image;
            img.Source = new BitmapImage(new Uri(string.Format("http://mars.jpl.nasa.gov/images/{0}", img.Tag)));
        }

        private void lbWhatsNew_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0) return;
            NewsItem itm = e.AddedItems[0] as NewsItem;
            this.NavigationService.Navigate(new Uri(String.Format("/Views/Learn/Article.xaml?id={0}", itm.NewsID), UriKind.Relative));
        }

        private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            DateTime dt = DateTime.Parse(tb.Text);
            tb.Text = dt.ToShortDateString();

        }

        private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            NavigationService.Navigate(new Uri(fe.Tag.ToString(), UriKind.Relative));
        }
    }
}
